<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Negative Binomial Sales Quota Example.</title>
<link rel="stylesheet" href="../../../../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../../index.html" title="Math Toolkit 4.2.1">
<link rel="up" href="../neg_binom_eg.html" title="Negative Binomial Distribution Examples">
<link rel="prev" href="neg_binom_size_eg.html" title="Estimating Sample Sizes for the Negative Binomial.">
<link rel="next" href="negative_binomial_example2.html" title="Negative Binomial Table Printing Example.">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="neg_binom_size_eg.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../neg_binom_eg.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="negative_binomial_example2.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h5 class="title">
<a name="math_toolkit.stat_tut.weg.neg_binom_eg.negative_binomial_example1"></a><a class="link" href="negative_binomial_example1.html" title="Negative Binomial Sales Quota Example.">Negative
          Binomial Sales Quota Example.</a>
</h5></div></div></div>
<p>
            This example program <a href="../../../../../../example/negative_binomial_example1.cpp" target="_top">negative_binomial_example1.cpp
            (full source code)</a> demonstrates a simple use to find the probability
            of meeting a sales quota.
          </p>
<p>
            Based on <a href="http://en.wikipedia.org/wiki/Negative_binomial_distribution" target="_top">a
            problem by Dr. Diane Evans, Professor of Mathematics at Rose-Hulman Institute
            of Technology</a>.
          </p>
<p>
            Pat is required to sell candy bars to raise money for the 6th grade field
            trip. There are thirty houses in the neighborhood, and Pat is not supposed
            to return home until five candy bars have been sold. So the child goes
            door to door, selling candy bars. At each house, there is a 0.4 probability
            (40%) of selling one candy bar and a 0.6 probability (60%) of selling
            nothing.
          </p>
<p>
            What is the probability mass (density) function (pdf) for selling the
            last (fifth) candy bar at the nth house?
          </p>
<p>
            The Negative Binomial(r, p) distribution describes the probability of
            k failures and r successes in k+r Bernoulli(p) trials with success on
            the last trial. (A <a href="http://en.wikipedia.org/wiki/Bernoulli_distribution" target="_top">Bernoulli
            trial</a> is one with only two possible outcomes, success of failure,
            and p is the probability of success). See also <a href="http://en.wikipedia.org/wiki/Bernoulli_distribution" target="_top">Bernoulli
            distribution</a> and <a href="http://www.math.uah.edu/stat/bernoulli/Introduction.xhtml" target="_top">Bernoulli
            applications</a>.
          </p>
<p>
            In this example, we will deliberately produce a variety of calculations
            and outputs to demonstrate the ways that the negative binomial distribution
            can be implemented with this library: it is also deliberately over-commented.
          </p>
<p>
            First we need to #define macros to control the error and discrete handling
            policies. For this simple example, we want to avoid throwing an exception
            (the default policy) and just return infinity. We want to treat the distribution
            as if it was continuous, so we choose a discrete_quantile policy of real,
            rather than the default policy integer_round_outwards.
          </p>
<pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_MATH_OVERFLOW_ERROR_POLICY</span> <span class="identifier">ignore_error</span>
<span class="preprocessor">#define</span> <span class="identifier">BOOST_MATH_DISCRETE_QUANTILE_POLICY</span> <span class="identifier">real</span>
</pre>
<p>
            After that we need some includes to provide easy access to the negative
            binomial distribution,
          </p>
<div class="caution"><table border="0" summary="Caution">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Caution]" src="../../../../../../../../doc/src/images/caution.png"></td>
<th align="left">Caution</th>
</tr>
<tr><td align="left" valign="top"><p>
              It is vital to #include distributions etc <span class="bold"><strong>after</strong></span>
              the above #defines
            </p></td></tr>
</table></div>
<p>
            and we need some std library iostream, of course.
          </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">distributions</span><span class="special">/</span><span class="identifier">negative_binomial</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
  <span class="comment">// for negative_binomial_distribution</span>
  <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">negative_binomial</span><span class="special">;</span> <span class="comment">// typedef provides default type is double.</span>
  <span class="keyword">using</span>  <span class="special">::</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">pdf</span><span class="special">;</span> <span class="comment">// Probability mass function.</span>
  <span class="keyword">using</span>  <span class="special">::</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">cdf</span><span class="special">;</span> <span class="comment">// Cumulative density function.</span>
  <span class="keyword">using</span>  <span class="special">::</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">quantile</span><span class="special">;</span>

<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
  <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span><span class="special">;</span> <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
  <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">noshowpoint</span><span class="special">;</span> <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">fixed</span><span class="special">;</span> <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">right</span><span class="special">;</span> <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">left</span><span class="special">;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iomanip</span><span class="special">&gt;</span>
  <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setprecision</span><span class="special">;</span> <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setw</span><span class="special">;</span>

<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">limits</span><span class="special">&gt;</span>
  <span class="keyword">using</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">;</span>
</pre>
<p>
            It is always sensible to use try and catch blocks because defaults policies
            are to throw an exception if anything goes wrong.
          </p>
<p>
            A simple catch block (see below) will ensure that you get a helpful error
            message instead of an abrupt program abort.
          </p>
<pre class="programlisting"><span class="keyword">try</span>
<span class="special">{</span>
</pre>
<p>
            Selling five candy bars means getting five successes, so successes r
            = 5. The total number of trials (n, in this case, houses visited) this
            takes is therefore = successes + failures or k + r = k + 5.
          </p>
<pre class="programlisting"><span class="keyword">double</span> <span class="identifier">sales_quota</span> <span class="special">=</span> <span class="number">5</span><span class="special">;</span> <span class="comment">// Pat's sales quota - successes (r).</span>
</pre>
<p>
            At each house, there is a 0.4 probability (40%) of selling one candy
            bar and a 0.6 probability (60%) of selling nothing.
          </p>
<pre class="programlisting"><span class="keyword">double</span> <span class="identifier">success_fraction</span> <span class="special">=</span> <span class="number">0.4</span><span class="special">;</span> <span class="comment">// success_fraction (p) - so failure_fraction is 0.6.</span>
</pre>
<p>
            The Negative Binomial(r, p) distribution describes the probability of
            k failures and r successes in k+r Bernoulli(p) trials with success on
            the last trial. (A <a href="http://en.wikipedia.org/wiki/Bernoulli_distribution" target="_top">Bernoulli
            trial</a> is one with only two possible outcomes, success of failure,
            and p is the probability of success).
          </p>
<p>
            We therefore start by constructing a negative binomial distribution with
            parameters sales_quota (required successes) and probability of success.
          </p>
<pre class="programlisting"><span class="identifier">negative_binomial</span> <span class="identifier">nb</span><span class="special">(</span><span class="identifier">sales_quota</span><span class="special">,</span> <span class="identifier">success_fraction</span><span class="special">);</span> <span class="comment">// type double by default.</span>
</pre>
<p>
            To confirm, display the success_fraction &amp; successes parameters of
            the distribution.
          </p>
<pre class="programlisting"><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Pat has a sales per house success rate of "</span> <span class="special">&lt;&lt;</span> <span class="identifier">success_fraction</span>
  <span class="special">&lt;&lt;</span> <span class="string">".\nTherefore he would, on average, sell "</span> <span class="special">&lt;&lt;</span> <span class="identifier">nb</span><span class="special">.</span><span class="identifier">success_fraction</span><span class="special">()</span> <span class="special">*</span> <span class="number">100</span>
  <span class="special">&lt;&lt;</span> <span class="string">" bars after trying 100 houses."</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>

<span class="keyword">int</span> <span class="identifier">all_houses</span> <span class="special">=</span> <span class="number">30</span><span class="special">;</span> <span class="comment">// The number of houses on the estate.</span>

<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"With a success rate of "</span> <span class="special">&lt;&lt;</span> <span class="identifier">nb</span><span class="special">.</span><span class="identifier">success_fraction</span><span class="special">()</span>
  <span class="special">&lt;&lt;</span> <span class="string">", he might expect, on average,\n"</span>
    <span class="string">"to need to visit about "</span> <span class="special">&lt;&lt;</span> <span class="identifier">success_fraction</span> <span class="special">*</span> <span class="identifier">all_houses</span>
    <span class="special">&lt;&lt;</span> <span class="string">" houses in order to sell all "</span> <span class="special">&lt;&lt;</span> <span class="identifier">nb</span><span class="special">.</span><span class="identifier">successes</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="string">" bars. "</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
</pre>
<pre class="programlisting">Pat has a sales per house success rate of 0.4.
Therefore he would, on average, sell 40 bars after trying 100 houses.
With a success rate of 0.4, he might expect, on average,
to need to visit about 12 houses in order to sell all 5 bars.
</pre>
<p>
            The random variable of interest is the number of houses that must be
            visited to sell five candy bars, so we substitute k = n - 5 into a negative_binomial(5,
            0.4) and obtain the <a class="link" href="../../../dist_ref/nmp.html#math_toolkit.dist_ref.nmp.pdf">Probability
            Density Function</a> of the distribution of houses visited. Obviously,
            the best possible case is that Pat makes sales on all the first five
            houses.
          </p>
<p>
            We calculate this using the pdf function:
          </p>
<pre class="programlisting"><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Probability that Pat finishes on the "</span> <span class="special">&lt;&lt;</span> <span class="identifier">sales_quota</span> <span class="special">&lt;&lt;</span> <span class="string">"th house is "</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">pdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="number">5</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span> <span class="comment">// == pdf(nb, 0)</span>
</pre>
<p>
            Of course, he could not finish on fewer than 5 houses because he must
            sell 5 candy bars. So the 5th house is the first that he could possibly
            finish on.
          </p>
<p>
            To finish on or before the 8th house, Pat must finish at the 5th, 6th,
            7th or 8th house. The probability that he will finish on <span class="bold"><strong>exactly</strong></span>
            ( == ) on any house is the Probability Density Function (pdf).
          </p>
<pre class="programlisting"><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Probability that Pat finishes on the 6th house is "</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">pdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="number">6</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Probability that Pat finishes on the 7th house is "</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">pdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="number">7</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Probability that Pat finishes on the 8th house is "</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">pdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="number">8</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
</pre>
<pre class="programlisting">Probability that Pat finishes on the 6th house is 0.03072
Probability that Pat finishes on the 7th house is 0.055296
Probability that Pat finishes on the 8th house is 0.077414
</pre>
<p>
            The sum of the probabilities for these houses is the Cumulative Distribution
            Function (cdf). We can calculate it by adding the individual probabilities.
          </p>
<pre class="programlisting"><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Probability that Pat finishes on or before the 8th house is sum "</span>
  <span class="string">"\n"</span> <span class="special">&lt;&lt;</span> <span class="string">"pdf(sales_quota) + pdf(6) + pdf(7) + pdf(8) = "</span>
  <span class="comment">// Sum each of the mass/density probabilities for houses sales_quota = 5, 6, 7, &amp; 8.</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">pdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="number">5</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">)</span> <span class="comment">// 0 failures.</span>
    <span class="special">+</span> <span class="identifier">pdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="number">6</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">)</span> <span class="comment">// 1 failure.</span>
    <span class="special">+</span> <span class="identifier">pdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="number">7</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">)</span> <span class="comment">// 2 failures.</span>
    <span class="special">+</span> <span class="identifier">pdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="number">8</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">)</span> <span class="comment">// 3 failures.</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
</pre>
<pre class="programlisting">pdf(sales_quota) + pdf(6) + pdf(7) + pdf(8) = 0.17367
</pre>
<p>
            Or, usually better, by using the negative binomial <span class="bold"><strong>cumulative</strong></span>
            distribution function.
          </p>
<pre class="programlisting"><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"\nProbability of selling his quota of "</span> <span class="special">&lt;&lt;</span> <span class="identifier">sales_quota</span>
  <span class="special">&lt;&lt;</span> <span class="string">" bars\non or before the "</span> <span class="special">&lt;&lt;</span> <span class="number">8</span> <span class="special">&lt;&lt;</span> <span class="string">"th house is "</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">cdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="number">8</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
</pre>
<pre class="programlisting">Probability of selling his quota of 5 bars on or before the 8th house is 0.17367
</pre>
<pre class="programlisting"><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"\nProbability that Pat finishes exactly on the 10th house is "</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">pdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="number">10</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"\nProbability of selling his quota of "</span> <span class="special">&lt;&lt;</span> <span class="identifier">sales_quota</span>
  <span class="special">&lt;&lt;</span> <span class="string">" bars\non or before the "</span> <span class="special">&lt;&lt;</span> <span class="number">10</span> <span class="special">&lt;&lt;</span> <span class="string">"th house is "</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">cdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="number">10</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
</pre>
<pre class="programlisting">Probability that Pat finishes exactly on the 10th house is 0.10033
Probability of selling his quota of 5 bars on or before the 10th house is 0.3669
</pre>
<pre class="programlisting"><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Probability that Pat finishes exactly on the 11th house is "</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">pdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="number">11</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"\nProbability of selling his quota of "</span> <span class="special">&lt;&lt;</span> <span class="identifier">sales_quota</span>
  <span class="special">&lt;&lt;</span> <span class="string">" bars\non or before the "</span> <span class="special">&lt;&lt;</span> <span class="number">11</span> <span class="special">&lt;&lt;</span> <span class="string">"th house is "</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">cdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="number">11</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
</pre>
<pre class="programlisting">Probability that Pat finishes on the 11th house is 0.10033
Probability of selling his quota of 5 candy bars
on or before the 11th house is 0.46723
</pre>
<pre class="programlisting"><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Probability that Pat finishes exactly on the 12th house is "</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">pdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="number">12</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>

<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"\nProbability of selling his quota of "</span> <span class="special">&lt;&lt;</span> <span class="identifier">sales_quota</span>
  <span class="special">&lt;&lt;</span> <span class="string">" bars\non or before the "</span> <span class="special">&lt;&lt;</span> <span class="number">12</span> <span class="special">&lt;&lt;</span> <span class="string">"th house is "</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">cdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="number">12</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
</pre>
<pre class="programlisting">Probability that Pat finishes on the 12th house is 0.094596
Probability of selling his quota of 5 candy bars
on or before the 12th house is 0.56182
</pre>
<p>
            Finally consider the risk of Pat not selling his quota of 5 bars even
            after visiting all the houses. Calculate the probability that he <span class="emphasis"><em>will</em></span>
            sell on or before the last house: Calculate the probability that he would
            sell all his quota on the very last house.
          </p>
<pre class="programlisting"><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Probability that Pat finishes on the "</span> <span class="special">&lt;&lt;</span> <span class="identifier">all_houses</span>
  <span class="special">&lt;&lt;</span> <span class="string">" house is "</span> <span class="special">&lt;&lt;</span> <span class="identifier">pdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="identifier">all_houses</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
</pre>
<p>
            Probability of selling his quota of 5 bars on the 30th house is
          </p>
<pre class="programlisting">Probability that Pat finishes on the 30 house is 0.00069145
</pre>
<p>
            when he'd be very unlucky indeed!
          </p>
<p>
            What is the probability that Pat exhausts all 30 houses in the neighborhood,
            and <span class="bold"><strong>still</strong></span> doesn't sell the required
            5 candy bars?
          </p>
<pre class="programlisting"><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"\nProbability of selling his quota of "</span> <span class="special">&lt;&lt;</span> <span class="identifier">sales_quota</span>
  <span class="special">&lt;&lt;</span> <span class="string">" bars\non or before the "</span> <span class="special">&lt;&lt;</span> <span class="identifier">all_houses</span> <span class="special">&lt;&lt;</span> <span class="string">"th house is "</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">cdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="identifier">all_houses</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
</pre>
<pre class="programlisting">Probability of selling his quota of 5 bars
on or before the 30th house is 0.99849
</pre>
<p>
            So the risk of failing even after visiting all the houses is 1 - this
            probability,
          </p>
<pre class="programlisting"><span class="number">1</span> <span class="special">-</span> <span class="identifier">cdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="identifier">all_houses</span> <span class="special">-</span> <span class="identifier">sales_quota</span></pre>
<p>
            But using this expression may cause serious inaccuracy, so it would be
            much better to use the complement of the cdf: So the risk of failing
            even at, or after, the 31th (non-existent) houses is 1 - this probability,
          </p>
<pre class="programlisting"><span class="number">1</span> <span class="special">-</span> <span class="identifier">cdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="identifier">all_houses</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">)</span></pre>
<p>
            But using this expression may cause serious inaccuracy. So it would be
            much better to use the __complement of the cdf (see <a class="link" href="../../overview/complements.html#why_complements">why
            complements?</a>).
          </p>
<pre class="programlisting"><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"\nProbability of failing to sell his quota of "</span> <span class="special">&lt;&lt;</span> <span class="identifier">sales_quota</span>
  <span class="special">&lt;&lt;</span> <span class="string">" bars\neven after visiting all "</span> <span class="special">&lt;&lt;</span> <span class="identifier">all_houses</span> <span class="special">&lt;&lt;</span> <span class="string">" houses is "</span>
  <span class="special">&lt;&lt;</span> <span class="identifier">cdf</span><span class="special">(</span><span class="identifier">complement</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="identifier">all_houses</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">))</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
</pre>
<pre class="programlisting">Probability of failing to sell his quota of 5 bars
even after visiting all 30 houses is 0.0015101
</pre>
<p>
            We can also use the quantile (percentile), the inverse of the cdf, to
            predict which house Pat will finish on. So for the 8th house:
          </p>
<pre class="programlisting"><span class="keyword">double</span> <span class="identifier">p</span> <span class="special">=</span> <span class="identifier">cdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="special">(</span><span class="number">8</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">));</span>
<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Probability of meeting sales quota on or before 8th house is "</span><span class="special">&lt;&lt;</span> <span class="identifier">p</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
</pre>
<pre class="programlisting">Probability of meeting sales quota on or before 8th house is 0.174
</pre>
<pre class="programlisting"><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"If the confidence of meeting sales quota is "</span> <span class="special">&lt;&lt;</span> <span class="identifier">p</span>
    <span class="special">&lt;&lt;</span> <span class="string">", then the finishing house is "</span> <span class="special">&lt;&lt;</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="identifier">p</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">sales_quota</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>

<span class="identifier">cout</span><span class="special">&lt;&lt;</span> <span class="string">" quantile(nb, p) = "</span> <span class="special">&lt;&lt;</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="identifier">p</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
</pre>
<pre class="programlisting">If the confidence of meeting sales quota is 0.17367, then the finishing house is 8
</pre>
<p>
            Demanding absolute certainty that all 5 will be sold, implies an infinite
            number of trials. (Of course, there are only 30 houses on the estate,
            so he can't ever be <span class="bold"><strong>certain</strong></span> of selling
            his quota).
          </p>
<pre class="programlisting"><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"If the confidence of meeting sales quota is "</span> <span class="special">&lt;&lt;</span> <span class="number">1.</span>
    <span class="special">&lt;&lt;</span> <span class="string">", then the finishing house is "</span> <span class="special">&lt;&lt;</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="number">1</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">sales_quota</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
<span class="comment">//  1.#INF == infinity.</span>
</pre>
<pre class="programlisting">If the confidence of meeting sales quota is 1, then the finishing house is 1.#INF
</pre>
<p>
            And similarly for a few other probabilities:
          </p>
<pre class="programlisting"><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"If the confidence of meeting sales quota is "</span> <span class="special">&lt;&lt;</span> <span class="number">0.</span>
    <span class="special">&lt;&lt;</span> <span class="string">", then the finishing house is "</span> <span class="special">&lt;&lt;</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="number">0.</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">sales_quota</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>

<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"If the confidence of meeting sales quota is "</span> <span class="special">&lt;&lt;</span> <span class="number">0.5</span>
    <span class="special">&lt;&lt;</span> <span class="string">", then the finishing house is "</span> <span class="special">&lt;&lt;</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="number">0.5</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">sales_quota</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>

<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"If the confidence of meeting sales quota is "</span> <span class="special">&lt;&lt;</span> <span class="number">1</span> <span class="special">-</span> <span class="number">0.00151</span> <span class="comment">// 30 th</span>
    <span class="special">&lt;&lt;</span> <span class="string">", then the finishing house is "</span> <span class="special">&lt;&lt;</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="number">1</span> <span class="special">-</span> <span class="number">0.00151</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">sales_quota</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
</pre>
<pre class="programlisting">If the confidence of meeting sales quota is 0, then the finishing house is 5
If the confidence of meeting sales quota is 0.5, then the finishing house is 11.337
If the confidence of meeting sales quota is 0.99849, then the finishing house is 30
</pre>
<p>
            Notice that because we chose a discrete quantile policy of real, the
            result can be an 'unreal' fractional house.
          </p>
<p>
            If the opposite is true, we don't want to assume any confidence, then
            this is tantamount to assuming that all the first sales_quota trials
            will be successful sales.
          </p>
<pre class="programlisting"><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"If confidence of meeting quota is zero\n(we assume all houses are successful sales)"</span>
  <span class="string">", then finishing house is "</span> <span class="special">&lt;&lt;</span> <span class="identifier">sales_quota</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
</pre>
<pre class="programlisting">If confidence of meeting quota is zero (we assume all houses are successful sales), then finishing house is 5
If confidence of meeting quota is 0, then finishing house is 5
</pre>
<p>
            We can list quantiles for a few probabilities:
          </p>
<pre class="programlisting"> <span class="keyword">double</span> <span class="identifier">ps</span><span class="special">[]</span> <span class="special">=</span> <span class="special">{</span><span class="number">0.</span><span class="special">,</span> <span class="number">0.001</span><span class="special">,</span> <span class="number">0.01</span><span class="special">,</span> <span class="number">0.05</span><span class="special">,</span> <span class="number">0.1</span><span class="special">,</span> <span class="number">0.5</span><span class="special">,</span> <span class="number">0.9</span><span class="special">,</span> <span class="number">0.95</span><span class="special">,</span> <span class="number">0.99</span><span class="special">,</span> <span class="number">0.999</span><span class="special">,</span> <span class="number">1.</span><span class="special">};</span>
 <span class="comment">// Confidence as fraction = 1-alpha, as percent =  100 * (1-alpha[i]) %</span>
 <span class="identifier">cout</span><span class="special">.</span><span class="identifier">precision</span><span class="special">(</span><span class="number">3</span><span class="special">);</span>
 <span class="keyword">for</span> <span class="special">(</span><span class="keyword">unsigned</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="keyword">sizeof</span><span class="special">(</span><span class="identifier">ps</span><span class="special">)/</span><span class="keyword">sizeof</span><span class="special">(</span><span class="identifier">ps</span><span class="special">[</span><span class="number">0</span><span class="special">]);</span> <span class="identifier">i</span><span class="special">++)</span>
 <span class="special">{</span>
   <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"If confidence of meeting quota is "</span> <span class="special">&lt;&lt;</span> <span class="identifier">ps</span><span class="special">[</span><span class="identifier">i</span><span class="special">]</span>
     <span class="special">&lt;&lt;</span> <span class="string">", then finishing house is "</span> <span class="special">&lt;&lt;</span> <span class="identifier">quantile</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="identifier">ps</span><span class="special">[</span><span class="identifier">i</span><span class="special">])</span> <span class="special">+</span> <span class="identifier">sales_quota</span>
     <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<pre class="programlisting">If confidence of meeting quota is 0, then finishing house is 5
If confidence of meeting quota is 0.001, then finishing house is 5
If confidence of meeting quota is 0.01, then finishing house is 5
If confidence of meeting quota is 0.05, then finishing house is 6.2
If confidence of meeting quota is 0.1, then finishing house is 7.06
If confidence of meeting quota is 0.5, then finishing house is 11.3
If confidence of meeting quota is 0.9, then finishing house is 17.8
If confidence of meeting quota is 0.95, then finishing house is 20.1
If confidence of meeting quota is 0.99, then finishing house is 24.8
If confidence of meeting quota is 0.999, then finishing house is 31.1
If confidence of meeting quota is 1, then finishing house is 1.#INF
</pre>
<p>
            We could have applied a ceil function to obtain a 'worst case' integer
            value for house.
          </p>
<pre class="programlisting"><span class="identifier">ceil</span><span class="special">(</span><span class="identifier">quantile</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="identifier">ps</span><span class="special">[</span><span class="identifier">i</span><span class="special">]))</span></pre>
<p>
            Or, if we had used the default discrete quantile policy, integer_outside,
            by omitting
          </p>
<pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_MATH_DISCRETE_QUANTILE_POLICY</span> <span class="identifier">real</span></pre>
<p>
            we would have achieved the same effect.
          </p>
<p>
            The real result gives some suggestion which house is most likely. For
            example, compare the real and integer_outside for 95% confidence.
          </p>
<pre class="programlisting">If confidence of meeting quota is 0.95, then finishing house is 20.1
If confidence of meeting quota is 0.95, then finishing house is 21
</pre>
<p>
            The real value 20.1 is much closer to 20 than 21, so integer_outside
            is pessimistic. We could also use integer_round_nearest policy to suggest
            that 20 is more likely.
          </p>
<p>
            Finally, we can tabulate the probability for the last sale being exactly
            on each house.
          </p>
<pre class="programlisting"><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"\nHouse for "</span> <span class="special">&lt;&lt;</span> <span class="identifier">sales_quota</span> <span class="special">&lt;&lt;</span> <span class="string">"th (last) sale.  Probability (%)"</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
<span class="identifier">cout</span><span class="special">.</span><span class="identifier">precision</span><span class="special">(</span><span class="number">5</span><span class="special">);</span>
<span class="keyword">for</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="special">(</span><span class="keyword">int</span><span class="special">)</span><span class="identifier">sales_quota</span><span class="special">;</span> <span class="identifier">i</span> <span class="special">&lt;</span> <span class="identifier">all_houses</span><span class="special">+</span><span class="number">1</span><span class="special">;</span> <span class="identifier">i</span><span class="special">++)</span>
<span class="special">{</span>
  <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">left</span> <span class="special">&lt;&lt;</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">3</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">i</span> <span class="special">&lt;&lt;</span> <span class="string">"                             "</span> <span class="special">&lt;&lt;</span> <span class="identifier">setw</span><span class="special">(</span><span class="number">8</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="identifier">cdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="identifier">i</span> <span class="special">-</span> <span class="identifier">sales_quota</span><span class="special">)</span>  <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
<span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
</pre>
<pre class="programlisting">House for 5 th (last) sale.  Probability (%)
5                               0.01024
6                               0.04096
7                               0.096256
8                               0.17367
9                               0.26657
10                              0.3669
11                              0.46723
12                              0.56182
13                              0.64696
14                              0.72074
15                              0.78272
16                              0.83343
17                              0.874
18                              0.90583
19                              0.93039
20                              0.94905
21                              0.96304
22                              0.97342
23                              0.98103
24                              0.98655
25                              0.99053
26                              0.99337
27                              0.99539
28                              0.99681
29                              0.9978
30                              0.99849
</pre>
<p>
            As noted above, using a catch block is always a good idea, even if you
            do not expect to use it.
          </p>
<pre class="programlisting"><span class="special">}</span>
<span class="keyword">catch</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">exception</span><span class="special">&amp;</span> <span class="identifier">e</span><span class="special">)</span>
<span class="special">{</span> <span class="comment">// Since we have set an overflow policy of ignore_error,</span>
  <span class="comment">// an overflow exception should never be thrown.</span>
   <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"\nMessage from thrown exception was:\n "</span> <span class="special">&lt;&lt;</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
</pre>
<p>
            For example, without a ignore domain error policy, if we asked for
          </p>
<pre class="programlisting"><span class="identifier">pdf</span><span class="special">(</span><span class="identifier">nb</span><span class="special">,</span> <span class="special">-</span><span class="number">1</span><span class="special">)</span></pre>
<p>
            for example, we would get:
          </p>
<pre class="programlisting">Message from thrown exception was:
 Error in function boost::math::pdf(const negative_binomial_distribution&lt;double&gt;&amp;, double):
 Number of failures argument is -1, but must be &gt;= 0 !
</pre>
</div>
<div class="copyright-footer">Copyright © 2006-2021 Nikhar Agrawal, Anton Bikineev, Matthew Borland,
      Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert Holin, Bruno
      Lalande, John Maddock, Evan Miller, Jeremy Murphy, Matthew Pulver, Johan Råde,
      Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, Daryle
      Walker and Xiaogang Zhang<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="neg_binom_size_eg.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../neg_binom_eg.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="negative_binomial_example2.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
